<!--
  - Copyright (C) 2018-2019
  - All rights reserved, Designed By www.samfeng.com
  - 注意：
  - 本软件为www.samfeng.com开发研制，项目使用请保留此说明
-->
<template>
  <div class="app-container">
      <avue-crud ref="crud"
                 :page="page"
                 :data="tableData"
                 :permission="permissionList"
                 :table-loading="tableLoading"
                 :option="tableOption"
                 :before-open="beforeOpen"
                 :before-close="beforeClose"
                 v-model="form"
                 @on-load="getPage"
                 @refresh-change="refreshChange"
                 @row-update="handleUpdate"
                 @row-save="handleSave"
                 @row-del="handleDel"
                 @sort-change="sortChange"
                 @search-change="searchChange"
                 @selection-change="selectionChange">
        <!--
        <template slot="menuLeft">
          <el-button type="success"
                     @click="batchShelf('1')"
                     size="small"
                     icon="el-icon-document"
                     v-if="checkPermi(['mall:goodsspu:edit'])">批量上架</el-button>
          <el-button type="warning"
                     @click="batchShelf('0')"
                     size="small"
                     icon="el-icon-document"
                     v-if="checkPermi(['mall:goodsspu:edit'])">批量下架</el-button>
        </template>-->
        <template slot="picUrls" slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.picUrls[0]"
            :preview-src-list="scope.row.picUrls">
          </el-image>
        </template>
        <template slot="salesPrice" slot-scope="scope">
          <div style="color: red">￥{{scope.row.salesPrice}}</div>
        </template>
        <template slot="status" slot-scope="scope">
          <el-switch
              active-value="1"
              inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeShelf(scope.row)">
          </el-switch>
        </template>
        <template slot="descriptionForm" slot-scope="">
          <BaseEditor v-model="currentSelectRow.description"/>
        </template>

        <template slot="headBntForm">
          <div>
            <el-button type="primary" @click="showStockQuant(currentSelectRow)"><i class="el-icon-s-data" />更新数量</el-button>
            <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-success" />执行补货</el-button>
          </div>
        </template>

        <template slot="productInfoForm">
          <div v-if="currentSelectRow">
            <el-collapse accordion @change="showDashBoard">
              <el-collapse-item>
                <template slot="title">
                  <div style="padding-left:50px">
                    <span>面板操作<i class="el-icon-date"></i></span>
                  </div>
                </template>              
                <el-row :gutter="12" style="text-align:center">
                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-refrigerator" />在手数量</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-refrigerator" />预测数量</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-shopping-cart-1" />已采购</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-sell" />已售</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-refresh-left" />订货规则</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-potato-strips" />物料清单</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)"><i class="el-icon-sort" />上架规则</el-button>
                      </div>
                      <div class="showTitle"><span>{{currentSelectRow.stock}}</span></div>
                    </el-card>
                  </el-col>

                  <el-col :span="5">
                    <el-card shadow="hover">
                      <div>
                        <el-button type="primary" @click="showNum(currentSelectRow.id)">
                          <div><i class="el-icon-set-up" />数量追溯</div>                 
                        </el-button>
                      </div>
                      <div class="trackTitle">
                          <div>
                            <span>进:{{currentSelectRow.stock}}</span>
                          </div>
                          <div>
                            <span>出:{{currentSelectRow.stock}}</span>
                          </div>
                      </div>
                    </el-card>
                  </el-col>            

                </el-row>
              </el-collapse-item>
            </el-collapse>
          </div>
        </template>  

        <template slot="picUrls" slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.picUrls?scope.row.picUrls[0]:''"
            :preview-src-list="scope.row.picUrls">
          </el-image>
        </template>

        <template slot="updateTimeForm" slot-scope="">
          <div style="margin-left:-90px">
            <el-tabs type="card" @tab-click="handleClickStatus">
                <el-tab-pane name="normal">
                    <span slot="label"><i class="el-icon-s-order"></i> 通用信息</span>
                </el-tab-pane>
                 <el-tab-pane name="attribute">
                    <span slot="label"><i class="el-icon-s-order"></i> 产品属性/参数</span>
                </el-tab-pane>
                <el-tab-pane name="stock">
                    <span slot="label"><i class="el-icon-truck"></i> 库存</span>
                </el-tab-pane>
                <el-tab-pane name="sale">
                    <span slot="label"><i class="el-icon-shopping-cart-full"></i> 销售</span>
                </el-tab-pane>
                <el-tab-pane name="invoice">
                    <span slot="label"><i class="el-icon-bank-card"></i> 开票</span>
                </el-tab-pane>
                <el-tab-pane name="remark">
                    <span slot="label"><i class="el-icon-document"></i> 备注</span>
                </el-tab-pane>
            </el-tabs>
          </div>
        </template>    

      </avue-crud>

    <el-dialog title="更新数量" :visible.sync="openStockQuant" width="90%" append-to-body>
        <avue-crud ref="crud1"
                  :page="page"
                  :data="stockQuantData"
                  :permission="stockQuantPermissionList"
                  :option="dialogStockQuantOption"
                  :cell-class-name="addCellClass"
                  v-model="stockQuantform"
                  @refresh-change="refreshStockQuant">
          <template slot-scope="{row,index}" slot="menu">
            <el-button
              type="success"
              size="small"
              icon="el-icon-search"
              >历史</el-button>            
            <el-button v-if="!row.tempSave||row.tempSave==null"
              type="danger"
              size="small"
              icon="el-icon-edit"
              @click="tempSave(row,index)"
              >应用</el-button>
            <el-button v-if="row.tempSave"
              type="warning"
              size="small"
              icon="el-icon-edit"
              @click="rowCell(row,index)"
              >设置</el-button>                       
            <el-button v-if="!row.tempSave||row.tempSave==null"
              type="danger"
              size="small"
              @click="stockRowCancel(row,index)"
              >取消</el-button>
            <el-button v-if="row.tempSave"  
              type="danger"
              size="small"
              @click="stockRowdel(row,index)"
              >删除</el-button>              
          </template>

          <template slot="inventoryQuantity" slot-scope="{row}">
            <el-input-number v-model="row.inventoryQuantity" size="mini" @change="caculationQty(row)" :disabled="row.tempSave?true:false"></el-input-number>
          </template>
          
          <template slot="inventoryDiffQuantity" slot-scope="{row}">
            <span>{{row.inventoryDiffQuantity}}</span>
          </template>          

        </avue-crud>        
    </el-dialog>

    <el-dialog title="选择库位" :visible.sync="openStocLocation" width="1000px"
    append-to-body>
      <stock-location-component
        ref="location"
        :stockLocationform="stockLocationform"
        :tableData="locationTableData"
        :tableLoading="tableLoading"
        :page="stockLocationPage"
        :paramsSearch="stockLocationParamsSearch"
        @locationSelectionChange="locationSelectionChange"
        @closeDialog="closeDialog"
        @getStockLocationPage="getStockLocationPage"
        @selectClick="selectClick"
        @closeClick="closeClick"
      >
      </stock-location-component>      
    </el-dialog>

  </div>
</template>

<script>
    import {checkPermi} from "@/utils/permission"
    import {getPage, getObj, addObj, putObj, delObj, putObjShelf} from '@/api/erp/product/product/product'
    import {
      delStockQuant,
      addStockQuant,
      updateStockQuant,
      getQuantPageInProductPage} from "@/api/erp/stock/quant/quant";
    import { getLocationPage } from "@/api/erp/stock/location/location";    
    import {tableOption, spec, attribute1} from '@/const/crud/erp/product_product'
    import BaseEditor from '@/components/Editor/index.vue'
    import stockLocationComponent from "@/views/erp/stock/quant/stockLocationComponent"

    export default {
        name: 'product',
        components: {
            BaseEditor,
            stockLocationComponent
        },
        data() {
            return {
              dialogStockQuantOption: {
                  addBtn:false,
                  editBtn:false,
                  dialogDrag:true,
                  addRowBtn:true,
                  delBtn: false,
                  cellBtn:false,
                  cancelBtn:false,
                  menuWidth:250,
                  column: [{
                              label: "位置id",
                              prop: "locationId",
                              display:false,
                              hide: true
                          },{
                              label: "位置",
                              prop: "locationName",
                              append: "选择",
                              appendClick:()=>{
                                this.openStocLocation = true                  
                              },                        
                              rules: [{
                                  required: true,
                                  message: '位置不能为空',
                                  trigger: 'blur'
                              }],
                              cell: true,
                              disabled:true
                          },{
                              label: "产品名称id",
                              prop: "productId",
                              display: false,
                              hide: true
                          },{
                              label: "产品名称",
                              prop: "productName",
                              display: true,
                              disabled: true,
                          },{
                              label: "可用数量",
                              dataType:'number',
                              prop: "availableQuantity",
                              display: true,
                              disabled: true,
                          },{
                              label: "在手数量",
                              prop: "quantity",
                              dataType:'number',
                              display: true,
                              disabled: true,
                          },{
                              label: "计数的数量",
                              prop: "inventoryQuantity",
                              dataType:'number',
                              display: true,
                              slot: true,
                              cell: true,
                          },{
                              label: "差异",
                              dataType:'number',
                              prop: "inventoryDiffQuantity",
                              disabled: true
                          },{
                              label: "安排日期",
                              prop: "inventoryDate",
                              display: true,
                              type: "datetime",
                              format: "yyyy-MM-dd HH:mm:ss",
                              valueFormat: "yyyy-MM-dd HH:mm:ss",
                              cell: true
                          }
                ]
              },
              currentSelectRow: {},
              openStocLocation: false,
              openStockQuant: false,
              openStockQuantIndex: 0,
              currentProductId: "",
              currentProductName: "",
              stockNum: 100,
              attribute: [],
              checkPermi: checkPermi,
              form: {},
              stockQuantform: {},
              stockLocationform: {},
              currentSelectLocation: {},
              tableData: [],
              locationTableData: [],
              stockQuantData: [],
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条
                ascs: [],//升序字段
                descs: 'create_time'//降序字段
              },
              stockLocationPage: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条
                ascs: [],//升序字段
                descs: 'create_time'//降序字段               
              },
              stock_quant_page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条
                ascs: [],//升序字段
                descs: 'create_time'//降序字段               
              },             
              paramsSearch: {},
              stockLocationParamsSearch: {
                usageType: "internal"
              },
              tableLoading: false,
              dialogAppraises: false,
              optionAppraises: {
                  props: {
                      avatar: 'nickName',
                      author: 'headimgUrl',
                      body: 'content'
                  }
              },
              selectionData: '',
              pointsConfig: null,
              tableOption: tableOption,
              attribute1: attribute1,
              spec: spec,
          }
        },
        watch:{                
        },
        created() {
        },
        computed: {
            permissionList() {
                return {
                    addBtn: checkPermi(['product:product:create']),
                    delBtn: checkPermi(['product:product:delete']),
                    editBtn: checkPermi(['product:product:update']),
                    viewBtn: checkPermi(['product:product:query'])
                };
            },
            stockQuantPermissionList() {
                return {
                  addBtn: checkPermi(['stock:quant:create']),
                  delBtn: checkPermi(['stock:quant:delete']),
                  editBtn: checkPermi(['stock:quant:update']),
                  viewBtn: checkPermi(['stock:quant:query'])
                };
            },            
        },
        mounted(){
        },
        methods: {
          showDashBoard() {
            // 面板数据
            console.log('showDashBoard====', this.currentProductId)
          },
          caculationQty(row) {
            if(row.availableQuantity==null) {
              row.availableQuantity = 0
            }
            if(row.quantity==null) {
              row.quantity = 0
            }             
            row.inventoryDiffQuantity = parseFloat(row.availableQuantity) - parseFloat(row.inventoryQuantity)
          },
          selectClick() {
            this.openStocLocation = false
            this.stockQuantData[this.openStockQuantIndex].locationName = this.currentSelectLocation.name
            const locationId = this.currentSelectLocation.id
            this.stockQuantData[this.openStockQuantIndex].locationId = locationId
            if (this.stockQuantData.length > 0){
              this.stockQuantData.forEach((item, index) => {
                if(item.locationId == locationId) {
                  this.stockQuantData[this.openStockQuantIndex].availableQuantity = item.availableQuantity
                  this.stockQuantData[this.openStockQuantIndex].quantity = item.quantity
                  return false
                }
              })
            }
            this.$refs.crud1.refreshTable()
          },
          closeClick() {
            this.openStocLocation = false
            this.currentSelectLocation = {}
          },
          locationSelectionChange(row) {
            let that = this
            if(row.length) {
              if(row.length >= 2) {
                that.currentSelectLocation = row[row.length-1]
                const nowVal = row.shift()
                that.$refs.location.$refs.location.toggleRowSelection(nowVal, false)
              } else {
                that.currentSelectLocation = row[0]
              }
            }
          },
          getStockLocationPage(page, params) {
            getLocationPage(Object.assign({
                current: page.currentPage,
                size: page.pageSize,
                descs: this.stockLocationPage.descs,
                ascs: this.stockLocationPage.ascs,
            }, params, this.stockLocationParamsSearch)).then(response => {
              this.locationTableData = response.data.list
              this.page.total = response.data.total
              this.page.currentPage = page.currentPage
              this.page.pageSize = page.pageSize
              this.tableLoading = false
            }).catch(() => {
              this.tableLoading = false
            })              
          },
          closeDialog() {
            this.openStocLocation = false
          },
          addCellClass({row,column}) {
            if (column.property == "locationName") {
              row.productId = this.currentProductId
              row.productName = this.currentProductName
            }
            this.openStockQuantIndex = row.$index
          },
          showStockQuant(row) {
            this.openStockQuant = true
            this.currentProductId = row.id
            this.currentProductName = row.name
            this.stockQuantPage(this.stock_quant_page)
          },
          showNum(id) {
            alert(id)
          },
          selectionChange(list){
            this.selectionData = list
          },
          batchShelf(shelf){
            if(this.selectionData.length<=0){
              this.$message.error('请选择商品')
              return
            }
            let selectionIds = ''
            this.selectionData.forEach(item => {
              selectionIds += item.id+ ','
            })
            this.putObjShelf(selectionIds, shelf)
          },
          changeShelf(row){
            this.putObjShelf(row.status, row.id)
          },
          putObjShelf(status, id){
              putObjShelf({
                id: id,
                status: status
              }).then(data => {
                this.getPage(this.page)
              })
          },
          beforeOpen(done,type){
            if(type == 'add'){
                done()
            }else if(type == 'edit'){
                this.tableLoading = true
                getObj(this.form.id).then(response => {
                  this.currentProductId = response.data.id
                  if(response.data.attributeDescription) {
                    this.$set(this.form,'attributeDescription', response.data.attributeDescription)
                  }
                  this.tableLoading = false
                  this.currentSelectRow = this.form
                  done()
                })
            }
          },
          beforeClose(done, type) {
            this.tableLoading = false
            done()
          },         
          searchChange(params,done) {
              params = this.filterForm(params)
              this.paramsSearch = params
              this.page.currentPage = 1
              this.getPage(this.page, params)
              done()
          },
          sortChange(val) {
              let prop = val.prop ? val.prop.replace(/([A-Z])/g, "_$1").toLowerCase() : '';
              if (val.order == 'ascending') {
                  this.page.descs = []
                  this.page.ascs = prop
              } else if (val.order == 'descending') {
                  this.page.ascs = []
                  this.page.descs = prop
              } else {
                  this.page.ascs = []
                  this.page.descs = []
              }
              this.getPage(this.page)
          },
          getPage(page, params) {
              this.tableLoading = true  
              getPage(Object.assign({
                  current: page.currentPage,
                  size: page.pageSize,
                  descs: this.page.descs,
                  ascs: this.page.ascs,
              }, params, this.paramsSearch)).then(response => {
                let tableData = response.data.list
                tableData.forEach((item,index) => {
                  item.attribute1 = item.attribute
                  item.spec = item.specification
                  item.picUrls = item.picUrls!=""?item.picUrls:[]
                  if(item.categoryId&&item.categoryId.length > 0) {
                    item.categoryId.forEach((iitem,iiindex) => {
                      item.categoryId[iiindex] = parseInt(iitem)
                    })
                  }
                })
                this.tableData = tableData
                this.page.total = response.data.total
                this.page.currentPage = page.currentPage
                this.page.pageSize = page.pageSize
                this.tableLoading = false
              }).catch(() => {
                this.tableLoading = false
              })
          },
          /**
           * 获取stock quant的数量
           */
          stockQuantPage(page) {
            this.tableLoading = true  
            getQuantPageInProductPage(Object.assign({
              pageNo: 1,
              pageSize: 20,
              productId: this.currentProductId
            })).then(response => {
              console.log(response)
              let stockQuantData = response.data.list
              stockQuantData.forEach(item => {
                item.tempSave = true
              })
              this.stockQuantData = stockQuantData
              this.stock_quant_page.total = response.data.total
              this.stock_quant_page.currentPage = page.currentPage
              this.stock_quant_page.pageSize = page.pageSize
              this.tableLoading = false
            }).catch(() => {
              this.tableLoading = false
            })
          },

          /**
           * @title 数据删除
           * @param row 为当前的数据
           * @param index 为当前删除数据的行数
           *
           **/
          handleDel: function (row, index) {
              var _this = this
              this.$confirm('是否确认删除此数据', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
              }).then(function () {
                  return delObj(row.id)
              }).then(data => {
                  _this.$message({
                      showClose: true,
                      message: '删除成功',
                      type: 'success'
                  })
                  this.getPage(this.page)
              }).catch(function (err) {
              })
          },
          handleClickStatus(tab, event) {
            let tableOption = this.tableOption
            if(tab.name == "normal") {
              tableOption.group[2].display = true
              tableOption.group[1].display = false
              tableOption.group[3].display = false
              tableOption.group[4].display = false
              tableOption.group[5].display = false
              // tableOption.group[6].display = false
            }
            if(tab.name == "attribute") {
              tableOption.group[2].display = false
              tableOption.group[1].display = false
              tableOption.group[3].display = false
              tableOption.group[4].display = false
              tableOption.group[5].display = true
              // tableOption.group[6].display = false
            }
            // if(tab.name == "mySpecification") {
            //   tableOption.group[2].display = false
            //   tableOption.group[1].display = false
            //   tableOption.group[3].display = false
            //   tableOption.group[4].display = false
            //   tableOption.group[5].display = false
            //   tableOption.group[6].display = true
            // }                            
            if(tab.name == "stock") {
              tableOption.group[3].display = true
              tableOption.group[2].display = false
              tableOption.group[1].display = false
              tableOption.group[4].display = false
              tableOption.group[5].display = false
              // tableOption.group[6].display = false
            }
            if(tab.name == "sale") {
              tableOption.group[4].display = true
              tableOption.group[1].display = false
              tableOption.group[2].display = false
              tableOption.group[3].display = false
              tableOption.group[5].display = false
              // tableOption.group[6].display = false
            }
            // if(tab.name == "invoice") {
            //   tableOption.group[2].display = true
            //   tableOption.group[1].display = false
            //   tableOption.group[3].display = false
            //   tableOption.group[4].display = false
            //   tableOption.group[5].display = false
            // }
            if(tab.name == "remark") {
              tableOption.group[1].display = true
              tableOption.group[2].display = false
              tableOption.group[3].display = false
              tableOption.group[4].display = false
              tableOption.group[5].display = false
              // tableOption.group[6].display = false
            }                                                  
          },
          /**
            * @title 数据更新
            * @param row 为当前的数据
            * @param index 为当前更新数据的行数
            * @param done 为表单关闭函数
            *
            **/
          handleUpdate: function (row, index, done, loading) {
              row.attribute = row.attribute1
              row.specification = row.spec
              // row.categoryFirst = row.categoryId[0]
              // row.categorySecond = row.categoryId[1]
              row.picUrls = row.picUrls?row.picUrls.split(','):""
              let categoryId = row.categoryId
              console.log(row)
              if (categoryId&&categoryId.length > 0) {
                row.lastCategoryId = categoryId[categoryId.length - 1]
              }
              putObj(row).then(data => {
                  this.$message({
                      showClose: true,
                      message: '修改成功',
                      type: 'success'
                  })
                  this.getPage(this.page)
                  done()
              }).catch((e) => {
                console.log(e)
                loading()
              })
          },
          stockQuantUpdate(row, index, done, loading) {
            console.log("update")
            console.log(row)
          },
          /**
            * @title 数据添加
            * @param row 为当前的数据
            * @param done 为表单关闭函数
            *
            **/
          handleSave: function (row, done, loading) {
            row.attribute = row.attribute1
            row.specification = row.spec
            let categoryId = row.categoryId
            if (categoryId&&categoryId.length > 0) {
              row.lastCategoryId = categoryId[categoryId.length - 1]
            }

            addObj(row).then(data => {
                this.$message({
                    showClose: true,
                    message: '添加成功',
                    type: 'success'
                })
                done()
                this.getPage(this.page)
            }).catch(() => {
                loading()
            })
          },

          /**
            * 刷新回调
            */
          refreshChange(page) {
            this.getPage(this.page)
          },
          refreshStockQuant() {
            this.stockQuantPage(this.stock_quant_page)
          },
          rowCell(row, index) {
            this.$refs.crud1.rowCell(row, index)
            row.tempSave = false
          },
          stockRowCancel(row,index) {
            row.$cellEdit = false
            row.tempSave = true
          },
          stockRowdel(row,index) {
            delStockQuant(row.id).then(res => {
                this.$message({
                    showClose: true,
                    message: '删除成功',
                    type: 'success'
                })
                this.stockQuantPage(this.stock_quant_page)                
            })
          },
          tempSave(row, index) {
            row.tempSave = true
            let stockQuantData = this.stockQuantData
            let duplicatIndex = 0
            let duplicat = false
            let inventoryQuantity = 0
            stockQuantData.forEach((item, index) => {
              if (index != row.$index) {
                if(item.locationId==row.locationId&&item.productId==row.productId) {
                  inventoryQuantity = parseFloat(row.inventoryQuantity)
                  item.inventoryQuantity = inventoryQuantity
                  item.inventoryDiffQuantity = parseFloat(item.availableQuantity) - inventoryQuantity
                  item.inventoryDate = row.inventoryDate
                  duplicat = true
                  duplicatIndex = index
                  return false
                }
              }
            })
            this.stockQuantData = stockQuantData
            row.$cellEdit = false
            
            if(duplicat) {
              this.stockQuantData.pop()              
              updateStockQuant(this.stockQuantData[duplicatIndex]).then(res => {
                this.$message({
                    showClose: true,
                    message: '修改成功',
                    type: 'success',
                })
                this.stockQuantPage(this.stock_quant_page)    
              })
            } else {
              //add
              console.log(row)
              if (row.id == null) {
                const qty = row.inventoryQuantity
                row.availableQuantity = qty
                row.quantity = qty
                row.reservedQuantity = 0
                row.inventoryDiffQuantity = 0
                row.inventoryQuantity = 0
                addStockQuant(row).then(res => {
                  this.$message({
                      showClose: true,
                      message: '添加成功',
                      type: 'success'
                  })
                  this.stockQuantPage(this.stock_quant_page)                     
                })
              } else {
                updateStockQuant(this.stockQuantData[duplicatIndex]).then(res => {
                    this.$message({
                        showClose: true,
                        message: '修改成功',
                        type: 'success'
                    })
                    this.stockQuantPage(this.stock_quant_page)    
                  })                 
              }
            }     
          },
          rowSave(row, index) {
            console.log(index)
            row.$index = index + 2
            console.log(row)
            this.$message.success(
              '新增数据' + JSON.stringify(row)
            )
            console.log(this.stockQuantData)
            this.stockQuantData.push(row)
            row.$cellEdit = false
          },
          rowUpdate(form, index, done) {
            this.$message.success(
              '编辑数据' + JSON.stringify(form) + '数据序号' + index
            )
            done()
          },
          handleRowClick(row, event, column) {
            this.$message({
              showClose: true,
              message: '序号' + row.$index,
              type: 'success',
            });
          }
      }
    }
</script>
<style scoped>
.el-col {
  border-radius: 4px;
  padding-bottom: 20px;
}
.wrapper {
  display: flex;
  justify-content: flex-start;
  width: 100%;
  flex-flow:row wrap;
}
.showTitle {
  font-size: 40px;
  color: red;
  padding-top: 10px;
}
.trackTitle {
  font-size: 20px;
  color: red;
  padding-top: 10px;
}
</style>
